package com.sqsf.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sqsf.javabean.Role;
import com.sqsf.javabean.Users;
import com.sqsf.service.RoleService;
import com.sqsf.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/users")
public class UserController {

    @Autowired
    UserService userService;

    @Autowired
    RoleService roleService;

    @GetMapping("/login")
    public String login() {
        return "login";
    }

    //用户登录
    @PostMapping("/login")
    public String login(HttpSession session, String username, String password) {
        Users users = userService.login(username,  password);
        if (users == null) {
            session.setAttribute("msg", "用户名或密码错误");
            return "login";
        }else {
            session.setAttribute("users", users);
            return "main";
        }
    }

    //用户登出
    @RequestMapping("/logout")
    public String logout(HttpSession session) {
        session.removeAttribute("users");
        return "login";
    }

    //获取所有用户
    @RequestMapping("/findAll")
    public String findAll(HttpServletRequest req,
                          @RequestParam(value = "page",required = false,defaultValue = "1") int page,
                          @RequestParam(value = "limit",required = false,defaultValue = "5") int limit){
        PageHelper.startPage(page, limit);
        List<Users> all = userService.findAll();

        PageInfo<Users> pageInfo=new PageInfo<>(all);
        req.setAttribute("pageInfo",pageInfo);

        return "/users/users-list";
    }

    //跳转添加页面
    //需要携带所有的角色信息
    //在进行添加时 需要选择添加用户对应的角色 所以跳转到添加页面时
    @GetMapping(value = "/add")
    public String add(HttpServletRequest req){
        List<Role> all = roleService.findAll();
        req.setAttribute("roleList",all);
        return "users/users-add";
    }

    //添加用户信息
    @PostMapping(value = "/add")
    public String add(Users users,int roleId){
        userService.add(users,roleId);
        return "redirect:/users/findAll";
    }

    //删除用户
    @RequestMapping("/delete")
    public String delete(int id ){
        userService.delete(id);
        return "redirect:/users/findAll";
    }

    //跳转角色修改页面
    @GetMapping(value = "/edit")
    public String edit(HttpServletRequest req,int id) {
        List<Role> all = roleService.findAll();
        Users  users = userService.FindById(id);
        req.setAttribute("roleList",all);
        req.setAttribute("users",users);
        return "/users/users-edit";
    }

    @PostMapping(value = "/edit")
    public String edit(Users users,int roleId) {
        userService.edit(users,roleId);
        return "redirect:/users/findAll";
    }


}
